package cn.iocoder.ny.module.mall.processor;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class OrderD implements Delayed {
    private final Long orderId; // Long 类型的 orderId
    private final long expireTime; // 订单过期的时间点
    private Long productId; // 产品ID
    private Long pspecId; // 规格ID
    private Integer quantity; // 库存数量

    public OrderD(Long orderId, long delay, TimeUnit unit) {
        this.orderId = orderId;
        this.expireTime = System.nanoTime() + unit.toNanos(delay);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(expireTime - System.nanoTime(), TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(Delayed other) {
        if (this == other) return 0;
        if (other instanceof OrderD) {
            OrderD o = (OrderD) other;
            return Long.compare(this.expireTime, o.expireTime);
        }
        long diff = this.getDelay(TimeUnit.NANOSECONDS) - other.getDelay(TimeUnit.NANOSECONDS);
        return (diff == 0) ? 0 : ((diff < 0) ? -1 : 1);
    }

    public Long getOrderId() {
        return orderId;
    }

    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public Long getPspecId() {
        return pspecId;
    }

    public void setPspecId(Long pspecId) {
        this.pspecId = pspecId;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }
}